<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
    <head>
        <title>Dynamic SVGIcon demo</title>
        <script type="text/javascript">

          var _gaq = _gaq || [];
          _gaq.push(['_setAccount', 'UA-17414889-3']);
          _gaq.push(['_trackPageview']);

          (function() {
            var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
          })();

        </script>
    </head>
    <body>

    <h2>Dynamic SVGIcon demo</h2>

    <p><img src="svgIconDemoScreenShot.png" align="right" />This is a minimal program which demonstrates a correct way to create SVG Icons and render them to the screen.  This particular example generates the SVG document dynamically.  You can also load SVG documents from file.</p>

    <p>This demonstrates simple user interaction with an SVGDiagram.  You can create and remove diagram elements, as well as alter diagram attributes.</p>

    <p>This example can be found in the SVG Salamnder source.</p>

<pre>
package com.kitfox.svg.example;

import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

import com.kitfox.svg.*;
import com.kitfox.svg.animation.*;
import com.kitfox.svg.app.beans.*;


class DynamicIconPanel extends JPanel
{
    public static final long serialVersionUID = 0;
    
    final SVGIcon icon;
    URI uri;
    
    LinkedList extraElements = new LinkedList();
    
    public DynamicIconPanel()
    {
        StringReader reader = new StringReader(makeDynamicSVG());
        uri = SVGCache.getSVGUniverse().loadSVG(reader, &quot;myImage&quot;); 
        icon = new SVGIcon();
        icon.setAntiAlias(true);
        icon.setSvgURI(uri);
        
        setPreferredSize(new Dimension(400, 400));
    }
    
    public void paintComponent(Graphics g)
    {
        final int width = getWidth();
        final int height = getHeight();
        
        g.setColor(getBackground());
        g.fillRect(0, 0, width, height);
        
        icon.paintIcon(this, g, 0, 0);
    }
    
    private String makeDynamicSVG()
    {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        
        pw.println(&quot;&lt;svg width=\&quot;400\&quot; height=\&quot;400\&quot; style=\&quot;fill:none;stroke-width:16\&quot;&gt;&quot;);
        pw.println(&quot;    &lt;circle id=\&quot;bigCircle\&quot; cx=\&quot;200\&quot; cy=\&quot;200\&quot; r=\&quot;200\&quot; style=\&quot;stroke:blue\&quot;/&gt;&quot;);
        pw.println(&quot;    &lt;g id=\&quot;extraElementGroup\&quot; style=\&quot;stroke-width:4;fill:green\&quot;/&gt;&quot;);
        pw.println(&quot;    &lt;text id=\&quot;userTextParent\&quot; x=\&quot;0\&quot; y=\&quot;40\&quot; style=\&quot;font-size:40;stroke:none;fill:red\&quot;&gt;&quot;);
        pw.println(&quot;        &lt;tspan id=\&quot;userText\&quot;&gt;Hello!&lt;/tspan&gt;&quot;);
        pw.println(&quot;    &lt;/text&gt;&quot;);
        pw.println(&quot;&lt;/svg&gt;&quot;);
        
        pw.close();
        return sw.toString();
    }
    
    public void setCircleForeground(Color color)
    {
        SVGDiagram diagram = SVGCache.getSVGUniverse().getDiagram(uri); 
        Circle circle = (Circle)diagram.getElement(&quot;bigCircle&quot;);
        
        String colorStrn = Integer.toHexString(color.getRGB() & 0xffffff);
        try
        {
            if (!circle.hasAttribute(&quot;stroke&quot;, AnimationElement.AT_CSS))
            {
                circle.addAttribute(&quot;stroke&quot;, AnimationElement.AT_CSS, &quot;#&quot; + colorStrn);
            }
            else
            {
                circle.setAttribute(&quot;stroke&quot;, AnimationElement.AT_CSS, &quot;#&quot; + colorStrn);
            }
        }
        catch (SVGElementException e)
        {
            e.printStackTrace();
        }
    }
    
    public void setCircleBackground(Color color)
    {
        SVGDiagram diagram = SVGCache.getSVGUniverse().getDiagram(uri); 
        Circle circle = (Circle)diagram.getElement(&quot;bigCircle&quot;);
        
        String colorStrn = Integer.toHexString(color.getRGB() & 0xffffff);
        try
        {
            if (!circle.hasAttribute(&quot;fill&quot;, AnimationElement.AT_CSS))
            {
                circle.addAttribute(&quot;fill&quot;, AnimationElement.AT_CSS, &quot;#&quot; + colorStrn);
            }
            else
            {
                circle.setAttribute(&quot;fill&quot;, AnimationElement.AT_CSS, &quot;#&quot; + colorStrn);
            }
        }
        catch (SVGElementException e)
        {
            e.printStackTrace();
        }
    }
    
    public void setText(String text)
    {
        SVGDiagram diagram = SVGCache.getSVGUniverse().getDiagram(uri); 
        Tspan tspan = (Tspan)diagram.getElement(&quot;userText&quot;);
        
        tspan.setText(text);
        
        Text textEle = (Text)diagram.getElement(&quot;userTextParent&quot;);
        
        try
        {
            textEle.rebuild();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    public void addCircle()
    {
        SVGDiagram diagram = SVGCache.getSVGUniverse().getDiagram(uri); 
        Group group = (Group)diagram.getElement(&quot;extraElementGroup&quot;);
        
        Circle circle = new Circle();
        try
        {
            int cx = (int)(Math.random() * 400);
            int cy = (int)(Math.random() * 400);
            
            circle.addAttribute(&quot;cx&quot;, AnimationElement.AT_XML, &quot;&quot; + cx);
            circle.addAttribute(&quot;cy&quot;, AnimationElement.AT_XML, &quot;&quot; + cy);
            circle.addAttribute(&quot;r&quot;, AnimationElement.AT_XML, &quot;10&quot;);
            
            group.loaderAddChild(null, circle);
            
            //Update animation state or group and it's decendants so that it reflects new animation values.
            // We could also call diagram.update(0.0) or SVGCache.getSVGUniverse().update().  Note that calling
            // circle.update(0.0) won't display anything since even though it will update the circle's state,
            // it won't update the parent group's state.
            group.updateTime(0.0);
            
            //Keep track of circles so we can remove them later
            extraElements.add(circle);
        }
        catch (SVGException e)
        {
            e.printStackTrace();
        }
        
    }
    
    public void addText()
    {
        SVGDiagram diagram = SVGCache.getSVGUniverse().getDiagram(uri); 
        Group group = (Group)diagram.getElement(&quot;extraElementGroup&quot;);
        
        Text text = new Text();
        try
        {
            int x = (int)(Math.random() * 300) + 50;
            int y = (int)(Math.random() * 300) + 50;
            
            text.addAttribute(&quot;x&quot;, AnimationElement.AT_XML, &quot;&quot; + x);
            text.addAttribute(&quot;y&quot;, AnimationElement.AT_XML, &quot;&quot; + y);
            text.addAttribute(&quot;font-size&quot;, AnimationElement.AT_CSS, &quot;12&quot;);
            
//            text.appendText(&quot;text&quot;);
            
            Tspan tspan = new Tspan();
            tspan.setText(&quot;text&quot;);
            text.appendTspan(tspan);
            
            group.loaderAddChild(null, text);
            text.build();
            
            //Update animation state or group and it's decendants so that it reflects new animation values.
            // We could also call diagram.update(0.0) or SVGCache.getSVGUniverse().update().  Note that calling
            // circle.update(0.0) won't display anything since even though it will update the circle's state,
            // it won't update the parent group's state.
            group.updateTime(0.0);
            
            //Keep track of circles so we can remove them later
            extraElements.add(text);
        }
        catch (SVGException e)
        {
            e.printStackTrace();
        }
        
    }
    
    public void removeElement()
    {
        int size = extraElements.size();
        if (size == 0) return;
        
        int idx = (int)(Math.random() * size);
        ShapeElement shapeElement = (ShapeElement)extraElements.remove(idx);
        
        SVGDiagram diagram = SVGCache.getSVGUniverse().getDiagram(uri); 
        Group group = (Group)diagram.getElement(&quot;extraElementGroup&quot;);
        
        try
        {
            group.removeChild(shapeElement);
        }
        catch (SVGException e)
        {
            e.printStackTrace();
        }
            
    }
}

/**
 *
 * @author  kitfox
 */
public class SVGIconDemoFrame extends javax.swing.JFrame
{
    public static final long serialVersionUID = 0;
    
    DynamicIconPanel panel = new DynamicIconPanel();
    
    /** Creates new form SVGIconDemo */
    public SVGIconDemoFrame()
    {
        initComponents();

        panel_display.add(panel, BorderLayout.CENTER);
        
        pack();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // &lt;editor-fold defaultstate=&quot;collapsed&quot; desc=&quot; Generated Code &quot;&gt;                          
    private void initComponents()
    {
        panel_display = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        bn_back = new javax.swing.JButton();
        bn_front = new javax.swing.JButton();
        bn_add = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        bn_remove = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        text_userText = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        panel_display.setLayout(new java.awt.BorderLayout());

        getContentPane().add(panel_display, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.Y_AXIS));

        bn_back.setText(&quot;Background&quot;);
        bn_back.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                bn_backActionPerformed(evt);
            }
        });

        jPanel3.add(bn_back);

        bn_front.setText(&quot;Foreground&quot;);
        bn_front.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                bn_frontActionPerformed(evt);
            }
        });

        jPanel3.add(bn_front);

        bn_add.setText(&quot;Add Circle&quot;);
        bn_add.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                bn_addActionPerformed(evt);
            }
        });

        jPanel3.add(bn_add);

        jButton1.setText(&quot;Add Text&quot;);
        jButton1.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel3.add(jButton1);

        bn_remove.setText(&quot;Remove&quot;);
        bn_remove.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                bn_removeActionPerformed(evt);
            }
        });

        jPanel3.add(bn_remove);

        jPanel2.add(jPanel3);

        jLabel1.setText(&quot;Text&quot;);
        jPanel1.add(jLabel1);

        text_userText.setText(&quot;Hello!&quot;);
        text_userText.setPreferredSize(new java.awt.Dimension(200, 20));
        text_userText.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                text_userTextActionPerformed(evt);
            }
        });

        jPanel1.add(text_userText);

        jPanel2.add(jPanel1);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        pack();
    }// &lt;/editor-fold&gt;                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)                                         
    {                                             
        panel.addText();
        repaint();
    }                                        

    private void text_userTextActionPerformed(java.awt.event.ActionEvent evt)                                              
    {                                                  
        panel.setText(text_userText.getText());
        repaint();
    }                                             

    private void bn_removeActionPerformed(java.awt.event.ActionEvent evt)                                          
    {                                              
        panel.removeElement();
        repaint();
    }                                         

    private void bn_addActionPerformed(java.awt.event.ActionEvent evt)                                       
    {                                           
        panel.addCircle();
        repaint();
    }                                      

    private void bn_frontActionPerformed(java.awt.event.ActionEvent evt)                                         
    {                                             
        panel.setCircleForeground(new Color((float)Math.random(), (float)Math.random(), (float)Math.random()));
        repaint();
    }                                        

    private void bn_backActionPerformed(java.awt.event.ActionEvent evt)                                        
    {                                            
        panel.setCircleBackground(new Color((float)Math.random(), (float)Math.random(), (float)Math.random()));
        repaint();        
    }                                       
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new SVGIconDemoFrame().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify                     
    private javax.swing.JButton bn_add;
    private javax.swing.JButton bn_back;
    private javax.swing.JButton bn_front;
    private javax.swing.JButton bn_remove;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel panel_display;
    private javax.swing.JTextField text_userText;
    // End of variables declaration                   
    
}
    
</pre>

  
</body>
</html>
